const path = require('path')

const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const zlib = require('zlib')
const isProduction = process.env.NODE_ENV === 'production'

function resolve(dir) {
    return path.join(__dirname, dir)
}
const timeStamp = new Date().getTime()

module.exports = {
    // baseUrl: './',
    assetsDir: 'static',
    productionSourceMap: false,
    devServer: {
        proxy: {
            '/api': {
                target: 'https://yl.weilankangyang.com',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': ''
                }
            }
        },
        public: 'localhost:8080' // 本地ip
    },
    publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
    outputDir: 'dist',
    assetsDir: 'static',
    runtimeCompiler: true,
    configureWebpack: {
        output: {
            filename: `static/js/[name].${timeStamp}.js`,
            chunkFilename: `static/js/[name].${timeStamp}.js`,
        },
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src'),
                '@i': path.resolve(__dirname, './src/assets'),
            }
        },
        plugins: [
            new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
            // 下面两项配置才是 compression-webpack-plugin 压缩配置
            // 压缩成 .gz 文件
            new CompressionPlugin({
                filename: '[path][base].gz',
                algorithm: 'gzip',
                test: /\.js$|\.css$|\.html$/,
                threshold: 10240,
                minRatio: 0.8
            }),
            // 压缩成 .br 文件，如果 zlib 报错无法解决，可以注释这段使用代码，一般本地没问题，需要注意线上服务器会可能发生找不到 zlib 的情况。
            // new CompressionPlugin({
            //     filename: '[path][base].br',
            //     algorithm: 'brotliCompress',
            //     test: /\.(js|css|html|svg)$/,
            //     compressionOptions: {
            //         params: {
            //             [zlib.constants.BROTLI_PARAM_QUALITY]: 11
            //         }
            //     },
            //     threshold: 10240,
            //     minRatio: 0.8
            // })
        ]
    },
    css: {
        // 输出重构 打包编译后的css文件名称，添加时间戳,存放在css/
        extract: {
            filename: `static/css/[name].${timeStamp}.css`,
            chunkFilename: `static/css/[name].${timeStamp}.css`,
        }
    },
    chainWebpack(config) {
        config.plugins.delete('preload') // TODO: need test
        config.plugins.delete('prefetch') // TODO: need test
        config.module
            .rule('images')
            .use('url-loader')
            .tap(options => {
                options.name = `static/img/[name].${timeStamp}.[ext]`
                options.fallback = {
                    loader: 'file-loader',
                    options: {
                        name: `static/img/[name].${timeStamp}.[ext]`
                    }
                }
                return options
            })
        // set svg-sprite-loader
        config.module
            .rule('svg')
            .exclude.add(resolve('src/assets/icons'))
            .end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/assets/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
            .end()
        config
            .when(process.env.NODE_ENV !== 'development',
                config => {
                    config.optimization.runtimeChunk('single'), {
                        from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
                        to: './', //到根目录下
                    }
                }
            )
    }
}